//
//  CWMoreVC.m
//  QQVoiceDemo
//
//  Created by Lim on 2021/2/19.
//  Copyright © 2021 陈旺. All rights reserved.
//

#import "CWMoreVC.h"
#import "SoundTouchOperation.h"
#import "CWAudioPlayer.h"
#import "CWRecordModel.h"

@interface CWMoreVC ()
{
    NSOperationQueue *_soundTouchQueue;
}
/// 声速
@property (weak, nonatomic) IBOutlet UISlider *soundSpeedSlider;
@property (weak, nonatomic) IBOutlet UILabel *soundSpeedLabel;

/// 音调
@property (weak, nonatomic) IBOutlet UISlider *tonesSlider;
@property (weak, nonatomic) IBOutlet UILabel *tonesLabel;

/// 速率
@property (weak, nonatomic) IBOutlet UISlider *rateSlider;
@property (weak, nonatomic) IBOutlet UILabel *rateLabel;

/// 采样率
@property (weak, nonatomic) IBOutlet UISlider *samplingRateSlider;
@property (weak, nonatomic) IBOutlet UILabel *samplingRateLabel;
@property (weak, nonatomic) IBOutlet UIButton *playerBtn;

@end

@implementation CWMoreVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[CWAudioPlayer shareInstance] addObserver:self forKeyPath:@"progress" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    
    
    [self setUpView];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

//当key路径对应的属性值发生改变时，监听器就会回调自身的监听方法，如下
- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary<NSKeyValueChangeKey,id> *)change
                       context:(void *)contex
{
    
}


- (void)setUpView
{
    self.soundSpeedLabel.text = [NSString stringWithFormat:@"%ld",(NSInteger)self.soundSpeedSlider.value];
    self.tonesLabel.text = [NSString stringWithFormat:@"%ld",(NSInteger)self.tonesSlider.value];
    self.rateLabel.text = [NSString stringWithFormat:@"%ld",(NSInteger)self.rateSlider.value];
    self.samplingRateLabel.text = [NSString stringWithFormat:@"%ld",(NSInteger)self.samplingRateSlider.value];
    [self initSoundTouchQueue];
}

- (void)initSoundTouchQueue {
    _soundTouchQueue = [[NSOperationQueue alloc] init];
    _soundTouchQueue.maxConcurrentOperationCount = 1;
}

#pragma mark - 变声功能
- (void)playAudioWithPath:(NSString *)path {
    NSData *data = [NSData dataWithContentsOfFile:path];
    MySountTouchConfig config;
    config.sampleRate = (int)self.samplingRateSlider.value; // 采样率8k - 44.1k
    config.tempoChange =  (int)self.soundSpeedSlider.value; // 速度 <速变不变调> -50 - 100
    config.pitch =  (int)self.tonesSlider.value; // 音调 -12 - 12
    config.rate = (int)self.rateSlider.value;    //声音速率 -50 - 100
    SoundTouchOperation *sdop = [[SoundTouchOperation alloc] initWithTarget:self
                                                                     action:@selector(playVoiceChange:)
                                                           SoundTouchConfig:config soundFile:data];
    [_soundTouchQueue cancelAllOperations];
    [_soundTouchQueue addOperation:sdop];
}

- (void)playVoiceChange:(NSString *)path {
    [[CWAudioPlayer shareInstance] playAudioWith:path];
}

- (IBAction)sliderValueChanged:(UISlider *)sender {
    NSLog(@"value: %lf", sender.value);
    switch (sender.tag) {
        case 100:// 声速
        {
            self.soundSpeedLabel.text = [NSString stringWithFormat:@"%ld",(NSInteger)sender.value];

        }
            break;
        case 200:// 音调
        {
            self.tonesLabel.text = [NSString stringWithFormat:@"%ld",(NSInteger)sender.value];

        }
            break;
        case 300: // 速率
        {
            self.rateLabel.text = [NSString stringWithFormat:@"%ld",(NSInteger)sender.value];

        }
            break;
        case 400: // 采样率
        {
            self.samplingRateLabel.text = [NSString stringWithFormat:@"%ld",(NSInteger)sender.value];

        }
            break;
            
        default:
            break;
    }
    
    [self setUpView];

}

- (IBAction)clickToPlayer:(UIButton *)sender {
    sender.selected = !sender.isSelected;
    if (sender.selected) {
        [sender setTitle: @"Stop" forState:UIControlStateNormal];
        [self playAudioWithPath:[CWRecordModel shareInstance].path];

    } else {
        [sender setTitle:@"Play" forState:UIControlStateNormal];
        [[CWAudioPlayer shareInstance] stopCurrentAudio]; // 停止播放音频

    }
}

- (IBAction)clickReSet:(UIButton *)sender {
    [[CWAudioPlayer shareInstance] stopCurrentAudio]; // 停止播放音频
    [self.playerBtn setTitle:@"Play" forState:UIControlStateNormal];
    self.samplingRateSlider.value = 11025; // 采样率8k - 44.1k
    self.soundSpeedSlider.value = 0; // 速度 <速变不变调> -50 - 100
    self.tonesSlider.value = 0; // 音调 -12 - 12
    self.rateSlider.value = 0;
    [self setUpView];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
